﻿// -----------------------------------------------------------------------
// <copyright file="History_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Linq.Expressions;
    using PMSGSC.Common;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class History_DAL :BaseDAL
    {
        // Lấy tất cả  History
        public static List< History> GetAll()
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB. Histories);
            return DB.Histories.ToList();
        }

        // Lấy tất cả  History
        public static List<History> GetAllByEquipment(int equipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Histories);
            return DB.Histories.Where(n=>n.Job.Equipment_ID == equipID).ToList();
        }

        // Lấy  History theo Id
        public static  History GetDataById(int id)
        {
            return DB.Histories.Where(m => m.History_ID == id).FirstOrDefault();
        }
        //Thêm một  History
        public static bool Insert( History  History)
        {
           
            try
            {
                DB.Histories.InsertOnSubmit( History);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một  History
        public static bool Delete( History  History)
        {
            try
            {
                DB.  Histories.DeleteOnSubmit( History);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        // Cập nhật  History
        public static bool Update( History  History)
        {
            try
            {
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        public static List< History> GetDataByEquipmentID(int id)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.  Histories);
            return DB.  Histories.Where(j => j.Job.Equipment_ID == id).ToList();
        }

        public static List<History> FilterHistory(int equipmentId, int workOrderId, int jobDescriptionId, string reportBy, DateTime From, DateTime To, int ShipID)
        {
            var predicate = PredicateBuilder.True<History>();
            predicate = predicate.And(n => n.WorkOrder.Equipment.Ship_ID == ShipID);
            if (equipmentId != 0)
                predicate = predicate.And(n => n.Job.Equipment_ID == equipmentId);
            if (workOrderId != 0)
                predicate = predicate.And(n => n.WorkOrder_ID == workOrderId);
            if(jobDescriptionId != 0)
                predicate = predicate.And(n => n.Job.JobDescription_ID == jobDescriptionId);
            if(reportBy != "")
                predicate = predicate.And(n => n.WorkOrder.ReportBy == reportBy);
            if(From != null && To != null)
                predicate = predicate.And(n => n.WorkOrder.ReportDate.Value.Date.CompareTo(From) >= 0 && n.WorkOrder.ReportDate.Value.Date.CompareTo(To) <= 0);


            return DB.Histories.Where(predicate).Cast<History>().ToList();

        }
    }
}
